package com.kdx.wallet.controller;

import com.kdx.wallet.Constant;
import com.kdx.wallet.entity.Wallet;
import com.kdx.wallet.entity.dto.CommonResponses;
import com.kdx.wallet.service.WalletService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/wallet")
public class WalletController {
    @Resource
    private WalletService walletService;

    /**
     * 查询用户钱包余额
     */
    @ResponseBody
    @GetMapping("/{userId}")
    public CommonResponses showYourBalance(@PathVariable Integer userId) {
        Wallet wallet = walletService.getAmount(userId);
        if (wallet != null) {
            return new CommonResponses<Wallet>(wallet, Constant.SUCCESS_CODE, Constant.SUCCESS_MSG);
        }
        return new CommonResponses<String>("failed",Constant.SHOW_YOUR_BALANCE_FAILED_CODE,Constant.FAILED_MSG);
    }

    /**
     * 用户充值
     */
    @ResponseBody
    @RequestMapping("/recharge/{userId}")
    public CommonResponses<String> recharge(@PathVariable Integer userId, Double amount) {
        boolean b = walletService.recharge(userId, amount);
        if (b) {
            return new CommonResponses<>("success", Constant.SUCCESS_CODE, Constant.SUCCESS_MSG);
        }
        return new CommonResponses<>("failed",Constant.RECHARGE_FAILED_CODE,Constant.FAILED_MSG);
    }

    /**
     * 用户提现
     */
    @ResponseBody
    @RequestMapping("/withdraw/{userId}")
    public CommonResponses<String> withdraw(@PathVariable Integer userId, Double amount) {
        boolean b = walletService.withdraw(userId, amount);
        if (b) {
            return new CommonResponses<>("success", Constant.SUCCESS_CODE, Constant.SUCCESS_MSG);
        }
        return new CommonResponses<>("failed",Constant.WITHDRAW_FAILED_CODE,Constant.FAILED_MSG);
    }

}
